import { Module, DynamicModule, Global } from '@nestjs/common';
import { AuditLogInterceptor, type IAuditLogService } from './audit-log.interceptor';

/**
 * 审计日志模块选项
 */
export interface AuditLogModuleOptions {
  /** 审计日志服务实例 */
  auditLogService: IAuditLogService;
}

/**
 * 审计日志模块
 * 
 * 提供审计日志拦截器，自动记录 HTTP 请求
 */
@Global()
@Module({})
export class AuditLogModule {
  static forRoot(options: AuditLogModuleOptions): DynamicModule {
    return {
      module: AuditLogModule,
      providers: [
        {
          provide: 'AUDIT_LOG_SERVICE',
          useValue: options.auditLogService,
        },
        {
          provide: AuditLogInterceptor,
          useFactory: (auditLogService: IAuditLogService) => {
            return new AuditLogInterceptor(auditLogService);
          },
          inject: ['AUDIT_LOG_SERVICE'],
        },
      ],
      exports: [AuditLogInterceptor],
    };
  }
}

